﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;
using System.Windows.Forms;

namespace MyVoiceControl.VoiceRecognition
{
    public class MyRecognizer
    {
        public MyRecognizer()
        { 
        
        }

        public void Init()
        {
            using (SpeechRecognitionEngine recognizer =
                new SpeechRecognitionEngine(
                    new System.Globalization.CultureInfo("en-US")))
            {
                recognizer.LoadGrammarAsync(MakeGrammer("Landing Gear"));
                recognizer.LoadGrammarAsync(MakeGrammer("Cargo Scoop"));
                recognizer.LoadGrammarAsync(MakeGrammer("Lights"));
                recognizer.LoadGrammarAsync(MakeGrammer("Weapons"));
                recognizer.LoadGrammarAsync(MakeGrammer("Impulse"));
                recognizer.LoadGrammarAsync(MakeGrammer("Jump Drive"));
                recognizer.LoadGrammarAsync(MakeGrammer("Super Cruise"));

                //// Create a grammar.
                //Choices cities = new Choices(new string[] {"Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });

                //GrammarBuilder gb = new GrammarBuilder();
                //gb.Append("Fly from");
                //gb.Append(cities);
                //gb.Append("to");
                //gb.Append(cities);

                //// Create a Grammar object and load it to the recognizer.
                //Grammar g = new Grammar(gb);
                //g.Name = ("City Chooser");
                //recognizer.LoadGrammarAsync(g);



                // Attach event handlers.
                recognizer.LoadGrammarCompleted +=
                  new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
                recognizer.SpeechDetected +=
                  new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
                recognizer.SpeechRecognized +=
                  new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);





                // Configure input to the speech recognizer.
                recognizer.SetInputToDefaultAudioDevice();

                // Start asynchronous, continuous speech recognition.
                recognizer.RecognizeAsync(RecognizeMode.Multiple);


                // Keep the console window open.
                while (true)
                {
                    Console.ReadLine();
                }
                recognizer.RecognizeAsyncStop();
            }

        }
        // Handle the SpeechDetected event.
        static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)
        {
            Console.WriteLine("Speech detected at AudioPosition = {0}", e.AudioPosition);
        }

        // Handle the LoadGrammarCompleted event.
        static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
        {
            Console.WriteLine("Grammar loaded: " + e.Grammar.Name);
        }

        // Handle the SpeechRecognized event.
        static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine("Speech recognized: " + e.Result.Text);
            switch (e.Result.Text)
            {
                case "Landing Gear":
                    Console.WriteLine("Landing Gear Command recognised: toggleing Gear and firing audio confirmation");
                    SendKeys.SendWait("T");
                    break;
                default:
                    break;
            }
        }

        Grammar MakeGrammer(string command)
        {
            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(command);
            Grammar g = new Grammar(gb) { Name = string.Format("{0}_Command", command) };
            return g;
        }
    }
}
